# 个人中心service层
from flask import jsonify
from werkzeug.datastructures import FileStorage
import os
import dao.PersonalDao as PersonalDao
import vo.PersonalVo as PersonalVo


# 用户中心趋势饼图，弃用
def service_personal_areasChart(uuid):
    # 数据可视化部分，弃用

    result = PersonalVo.vo_personal_areasChart(uuid)
    return jsonify(result)


# 改昵称
def service_personal_changeNickName(uuid, newNickName):
    result_dao = PersonalDao.dao_personal_changeNickName(uuid, newNickName)
    result = PersonalVo.vo_personal_changeNickName(result_dao)
    return jsonify(result)


# 改头像
def service_personal_changeAvatar(uuid, newAvatar):
    abs_url = '/www/wwwroot/http/imgs/avtar'
    db_abs_url = "http://43.140.221.237:9999/imgs/avtar/"
    avatar_url = os.path.join(abs_url, str(uuid) + '.jpg')
    db_avatar_url = db_abs_url + str(uuid) + ".jpg"

    FileStorage(newAvatar).save(avatar_url)

    if os.path.exists(avatar_url):
        result_dao = PersonalDao.dao_personal_changeAvatar(uuid, db_avatar_url)
        result = PersonalVo.vo_personal_changeAvatar(result_dao)
        return jsonify(result)
    else:
        result = PersonalVo.vo_personal_default_fail()
        return jsonify(result)


# 用户中心提交记录，弃用
def service_personal_submitLog(uuid):
    result_dao = PersonalDao.dao_personal_submitLog(uuid)
    result = PersonalVo.vo_personal_submitLog(result_dao)
    return jsonify(result)


# 提交日历图，弃用
def service_personal_uploadChart(uuid, year):
    result_dao = PersonalDao.dao_personal_uploadChart(uuid, year)
    result = PersonalVo.vo_personal_uploadChart(result_dao)
    return jsonify(result)


# 删除文章
def service_personal_deleteArticle(articleId):
    result_dao = PersonalDao.dao_personal_deleteArticle(articleId)
    result = PersonalVo.vo_personal_deleteArticle(result_dao)
    return jsonify(result)


# 上传文章
def service_personal_submitArticle(content, uuid, title, coverImage):
    result_dao = PersonalDao.dao_personal_submitArticle(content, uuid, title, coverImage)
    result = PersonalVo.vo_personal_submitArticle(result_dao)
    return jsonify(result)


# 文章修改
def service_personal_changeArticle(content, uuid, articleId, title, coverImage):
    result_dao = PersonalDao.dao_personal_changeArticle(content, uuid, articleId, title, coverImage)
    result = PersonalVo.vo_personal_changeArticle(result_dao)
    return jsonify(result)


# 我发布的文章
def service_personal_myArticle(pagination, amount, rule, uuid):
    result_dao = PersonalDao.dao_personal_myArticle(pagination, amount, rule, uuid)
    result = PersonalVo.vo_personal_myArticle(result_dao)
    return jsonify(result)
